<?php
require_once('../classes/smarty_setup.php');
require_once('../classes/mofachi.User.php');
$smarty = new Smarty_mofachi();

@ $act = $_REQUEST[ "act" ];
if( $act == "add" ) {
	addUser();
} else if( $act == "remove" ) {
	removeUser();
} else if( $act == "get" ) {
	getUser( $smarty );
} else if( $act == "update" ) {
	updateUser();
}
$smarty->assign( 'act', $act );

listUser( $smarty );
$smarty->display('t01_user.htm');

function listUser( $smarty ) {
	$result = User::listUser( 0, 20 );
	$smarty->assign( 'res', $result );
}
function addUser() {
	@ $email = $_POST[ 'email' ];
	@ $password = $_POST[ 'password' ];
	if( !$email || !$password )
		return;
	
	$user = new User();
	$user->email = $email;
	$user->password = $password;
	$user->add();
}
function removeUser() {
	@ $delist = $_POST[ "delist" ];
	if( !$delist )
		return;

	$id_array = explode( ",", $delist );
	foreach( $id_array as $key => $value ) {
		$user = new User();
		$user->user_id = $value;
		$user->remove();
	}
}
function getUser( $smarty ) {	
	@ $uid = $_GET[ "uid" ];
	if( !$uid )
		return;

	$user = new User();
	$user->user_id = $uid;
	$user->loadById();
	$smarty->assign( 'row', $user );
}
function updateUser() {
	@ $uid = $_POST[ "uid" ];
	@ $email = $_POST[ 'email' ];
	if( !$email || !$uid )
		return;
	
	$user = new User();
	$user->user_id = $uid;
	$user->loadById();
	$user->email = $email;
	$user->update();
}
?>